//
//  TTQualityCheckPopListView.m
//  TaoTuo
//
//  Created by 周晨晔 on 2019/9/25.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTQualityCheckPopListView.h"
#import "TTQualityCheckViewCell.h"

static NSString *qcCellID = @"qcCellID_key";

@interface TTQualityCheckPopListView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *rightBtn;
@property (nonatomic, strong) UIButton *closeBtn;

@property (nonatomic, strong) UIView *headerView;

@property (nonatomic, weak) UIView *lastView;


@property (nonatomic, strong) TTQualityCheckModel *selectModel;

@end

@implementation TTQualityCheckPopListView

+ (instancetype)popListViewWithLastView:(UIView *)lastView
{
    return [[self alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 40, 450) lastView:lastView];
}

- (instancetype)initWithFrame:(CGRect)frame lastView:(UIView *)lastView
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.lastView = lastView;
        [self setupSubviews];
        
    }
    return self;
}

- (void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
    // 默认选中第一个
    TTQualityCheckModel *firstModel = dataArray.firstObject;
    firstModel.isSelect = YES;
    self.selectModel = firstModel;
    
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTQualityCheckViewCell *cell = [tableView dequeueReusableCellWithIdentifier:qcCellID];
    cell.model = self.dataArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    @weakify(self)
    cell.chooseBlock = ^(TTQualityCheckModel * _Nonnull model) {
        @strongify(self)
        [self setSelectModelWithModel:model];
    };
    return cell;
}

- (void)setSelectModelWithModel:(TTQualityCheckModel *)model
{
    for (TTQualityCheckModel *qcModel in self.dataArray) {
        qcModel.isSelect = NO;
    }
    // 选中的model
    model.isSelect = YES;
    self.selectModel = model;
    [self.tableView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTQualityCheckModel *model = self.dataArray[indexPath.row];
    if (model.isSelect) {
        return;
    }
    [self setSelectModelWithModel:model];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [self setupHeaderView];
}


// 完成
- (void)nextPageAction
{
    [self.lastView dismissWithAnimationDuration:0];
    [self dismissWithAnimationDuration:0.3];
    if (self.hiddenLeftBtn) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"orderPageModifyQualityCheckNotify_key" object:self.selectModel];
    } else {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"chooseFinishedQualityCheckNotify_key" object:self.selectModel];
    }
    
}

// 上一步
- (void)lastPageAction
{
    self.lastView.hidden = NO;
    [self dismissWithAnimationDuration:0.3];
}

// 关闭页面
- (void)closeBtnAction
{
    self.lastView.hidden = NO;
    [self dismissWithAnimationDuration:0.3];
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"closePopView_key" object:nil];
}

- (void)setupSubviews
{
    _closeBtn = [UIButton createButtonWithImage:[UIImage imageNamed:@"close_white"] target:self action:@selector(closeBtnAction)];
    [self addSubview:_closeBtn];
    [_closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.centerX.equalTo(self);
    }];
}

- (UIView *)setupHeaderView
{
    if (self.headerView) {
        return self.headerView;
    }
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 40)];
    headerView.backgroundColor = [UIColor whiteColor];
    self.headerView = headerView;
    
    _leftBtn = [UIButton createButtonWithTitle:@"上一步" titleColor:UIColorHex(666666) titleFont:[UIFont systemFontOfSize:13] backgroundColor:nil target:self action:@selector(lastPageAction)];
    [headerView addSubview:_leftBtn];
    [_leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.centerY.equalTo(headerView);
    }];
    
    _leftBtn.hidden = self.hiddenLeftBtn;
    
    _titleLabel = [UILabel labelWithTitle:@"证书要求" color:UIColorHex(000000) fontSize:15];
    [headerView addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(headerView);
    }];
    
    _rightBtn = [UIButton createButtonWithTitle:@"完成" titleColor:kMainColor titleFont:[UIFont systemFontOfSize:13] backgroundColor:nil target:self action:@selector(nextPageAction)];
    [headerView addSubview:_rightBtn];
    [_rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(headerView).mas_offset(-10);
        make.centerY.equalTo(headerView);
    }];
    
    return self.headerView;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height - 50) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.layer.cornerRadius = 10;
        _tableView.rowHeight = 60;
        _tableView.tableFooterView = [UIView new];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TTQualityCheckViewCell class]) bundle:nil] forCellReuseIdentifier:qcCellID];
        [self addSubview:_tableView];
        
    }
    return _tableView;
    
}

- (void)dealloc
{
    NSLog(@"移除");
}

@end
